/**
 * Created by Jason on 2015/2/12.
 */
/*自定义插件*/
(function($){
    /*点击选择插件*/
    $.fn.extend({
        /*单选*/
        selectOne:function(thenDo){
            var selector = this.selector;
            $('body').off('click.myhouse.selectOne'+selector);
            $('body').on('click.myhouse.selectOne'+selector,selector,[selector],function(e){
                var _this = $(this),
                    all = $(e.data[0]),
                    dataId = _this.data('id'),
                    name = _this.data('name'),
                    selector = '[name='+name+']';

                $(selector).val(dataId);

                all.removeClass('active');
                _this.addClass('active');

                if(thenDo != undefined)
                    thenDo(this,dataId,name)
		 
            });
        },

        /*多选*/
        selectMuti:function(){
            this.off('click.myhouse.selectMuti');
            this.on('click.myhouse.selectMuti',function(){
                var _this = $(this),
                    id = _this.data('id'),
                    name = _this.data('name');

                _this.toggleClass('select');
                var input = _this.parent().find('input').filter(function(){
                    return $(this).val() == id;
                });
				 
                if(input.length){
                    input.remove();
                }else{
                    _this.parent().append('<input type="hidden" name="'+name+'[]" value="'+id+'" />');
                }
			 check();
            });
        },

        /*多选文字*/
        selectWords:function(){
            var all = this;
            this.off('click.myhouse.selectWords');
            this.on('click.myhouse.selectWords',function(){
                var _this = $(this),
                    name = _this.data('name');

                _this.toggleClass('active');

                var texts = [];
                var selectedItems = all.filter('.active');

                for (var i = 0; i < selectedItems.length; i++) {
                    texts.push($(selectedItems[i]).data('text'));
                }

                $('[name='+name+']').val(texts.join(','));
            });
        }
    });

    /**
     * confirm 晚绑定
     */
    $.fn.extend({
        confirm:function(title,onConfirm){
            var _this = $(this[0]);
            var confirmed = _this.data('confirmed');
            if(!confirmed){
                _this.confirmation({
                    placement:'top',
                    title:title,
                    btnOkLabel:'<i class="icon-ok-sign icon-white"></i> 确定',
                    btnCancelLabel:'<i class="icon-remove-sign"></i> 取消',
                    onConfirm:onConfirm
                })
                _this.data('confirmed',true);
                _this.confirmation('show');
            }
        }
    });

    /**
     * 更新进度条
     */
    $.fn.extend({
        updateProgress:function(proportion ,report){
            this.find('.progress-bar').css('width',proportion + '%');
            this.find('.progress-bar span').text(report);
        }
    });

    /**
     * 渲染模版
     */
    $.extend({
        renderTmpl:function(tmpl, values){
            if(tmpl == undefined)
                return '';

            var elements = tmpl,
                param = null;

            for(param in values){
                var reg = param.replace(/\-/g,'\\-');
                reg = new RegExp('\\{\\{' + reg + '\\}\\}', 'g');

                elements = elements.replace(reg, values[param] == undefined ?'':values[param]);
            }

            return elements;
        }
    })

})(jQuery);

 